/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.facilitieselements;

import org.openaecfm.model.actor.Person;
import org.openaecfm.model.cost.CostValue;
import org.openaecfm.model.datetime.Date;
import org.openaecfm.model.kernel.Group;
import org.openaecfm.model.facilitieselements.Asset;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class AssetIO extends AbstractEntityInstanceInputImpl<Asset>
		implements IEntityInstanceOutput<Asset> {
	private IEntityInstanceUtilInput<Group> utilInput;
	private IEntityInstanceUtilOutput<Group> utilOutput;

	public void setUtilOutput(IEntityInstanceUtilOutput<Group> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(Asset pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCASSET",
				appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set Identification
		if (pojo.getIdentification() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getIdentification(),
					StringParameter.UTF_8));
		}

		// Set OriginalValue
		if (pojo.getOriginalValue() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getOriginalValue())));
		}

		// Set CurrentValue
		if (pojo.getCurrentValue() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getCurrentValue())));
		}

		// Set TotalReplacementCost
		if (pojo.getTotalReplacementCost() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getTotalReplacementCost())));
		}

		// Set Owner
		if (pojo.getOwner() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getOwner())));
		}

		// Set User
		if (pojo.getUser() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getUser())));
		}

		// Set ResponsiblePerson
		if (pojo.getResponsiblePerson() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getResponsiblePerson())));
		}

		// Set IncorporationDate
		if (pojo.getIncorporationDate() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getIncorporationDate()
					.toString(), StringParameter.UTF_8));
		}

		// Set DepreciatedValue
		if (pojo.getDepreciatedValue() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getDepreciatedValue())));
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Asset doCreateObject() {
		return new Asset();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, Asset pojo,
			IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set OriginalValue
		Parameter orvalue = sei.getParameter(6);
		if (orvalue.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) orvalue;
			pojo.setOriginalValue((CostValue) appDataManager.getObject(e
					.toInstanceName()));
		}

		// Set CurrentValue
		Parameter cuvalue = sei.getParameter(7);
		if (cuvalue.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) cuvalue;
			pojo.setCurrentValue((CostValue) appDataManager.getObject(e
					.toInstanceName()));
		}

		// Set TotalReplacementCost
		Parameter totalcost = sei.getParameter(8);
		if (totalcost.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) totalcost;
			pojo.setTotalReplacementCost((CostValue) appDataManager.getObject(e
					.toInstanceName()));
		}

		// Set Owner
		Parameter owner = sei.getParameter(9);
		if (owner.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) owner;
			pojo.setOwner(appDataManager.getObject(e.toInstanceName()));
		}

		// Set User
		Parameter user = sei.getParameter(10);
		if (user.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) user;
			pojo.setUser(appDataManager.getObject(e.toInstanceName()));
		}

		// Set ResponsiblePerson
		Parameter resperson = sei.getParameter(11);
		if (resperson.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) resperson;
			pojo.setResponsiblePerson((Person) appDataManager.getObject(e
					.toInstanceName()));
		}

		// Set DepreciatedValue
		Parameter devalue = sei.getParameter(13);
		if (devalue.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) devalue;
			pojo.setDepreciatedValue((CostValue) appDataManager.getObject(e
					.toInstanceName()));
		}
	}

	public void setUtilInput(IEntityInstanceUtilInput<Group> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei, Asset pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		utilInput.resolveNotReferenceParameter(ei, pojo);

		// Set Identification
		Parameter identification = sei.getParameter(5);
		if (identification.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) identification;
			pojo.setIdentification(s.toUTF8());
		}

		// Set IncorporationDate
		Parameter incordate = sei.getParameter(12);
		if (incordate.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) incordate;
			pojo.setIncorporationDate(new Date(s.toUTF8()));
		}
	}
}
